package com.cgsd.heyu.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.android.library.utils.ToastHelper;
import com.cgsd.heyu.R;
import com.cgsd.heyu.common.CommonConfig;

/**
 * @author xiaoxing
 * Description 创建分类
 * CreateData: 2020/11/3
 */
public class CreateClassifyDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private DialogListener dialogListener;
    private EditText edtContent;
    private TextView tvClassifySize;

    public void setDialogListener(DialogListener dialogListener) {
        this.dialogListener = dialogListener;
    }

    public CreateClassifyDialog(@NonNull Context context) {
        super(context, R.style.dialog_custom);
        this.mContext = context;
        setContentView(R.layout.dialog_create_classify);
        Window window = this.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
        }
        setCanceledOnTouchOutside(false);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        // 获取屏幕宽、高用
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();
        // 宽度设置为屏幕的0.7
        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);
        initView();
    }

    private void initView() {
        edtContent = findViewById(R.id.edt_content);
        tvClassifySize = findViewById(R.id.tv_classify_size);
        findViewById(R.id.tv_confirm).setOnClickListener(this);
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        tvClassifySize.setText(mContext.getString(R.string.create_classify_tips, CommonConfig.getInstance().classifySize));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                confirm();
                break;
            default:
        }
    }


    /**
     * 点击确认
     */
    private void confirm(){
        String content = edtContent.getText().toString();
        if (content.length() <= 0){
            ToastHelper.showDefaultToast(mContext.getString(R.string.input_empty));
            return;
        }
        String empty = " ";
        if (content.contains(empty)){
            ToastHelper.showDefaultToast(mContext.getString(R.string.not_allow_space));
            return;
        }
        if (dialogListener != null){
            dialogListener.confirm(content);
        }
    }

    @Override
    public void show() {
        super.show();
        if (edtContent != null){
            edtContent.setText("");
        }
    }

    public interface DialogListener{
        /**
         * 确认
         * @param content
         */
        void confirm(String content);
    }

}
